interface Post {
    x: number;
    y: number;
}
/**元素拖动类 */
export default class DomDrag {
    domrect: DOMRect = new DOMRect();
    post: Post = { x: 0, y: 0 };
    downpst: Post = { x: 0, y: 0 };
    constructor(private readonly element: HTMLElement) {
        this.init();
    }
    /**设置传入的元素拖动 */
    static setDrag(element: HTMLElement) {
        new DomDrag(element);
    }
    init() {
        if (this.element !== null) {
            this.element.onmousedown = this.onDown.bind(this);
            this.element.style.zIndex = "3";
            // this.element.style.cursor = "move";
        }
    }
    onDown(ent: MouseEvent) {
        window.document.onmousemove = this.onMove.bind(this);
        window.document.onmouseup = this.onUP.bind(this);
        this.domrect = this.element.getBoundingClientRect();
        this.downpst.x = ent.x;
        this.downpst.y = ent.y;
        // console.log("yztDown", ent, this.domrect);
    }
    onMove(ent: MouseEvent) {
        const x = ent.x - this.downpst.x;
        const y = ent.y - this.downpst.y;
        this.post.x += x;
        this.post.y += y;
        this.setPost();
        this.downpst.x = ent.x;
        this.downpst.y = ent.y;
    }
    onUP() {
        window.document.onmousemove = null;
    }
    setPost() {
        this.element.style.transform = `translate(${this.post.x}px, ${this.post.y}px)`;
    }
}
